package com.icinfo.cloud.provider.punish.common.seal.utils;

import com.timvale.sealplatform.sdk.ClientManageService;
import com.timvale.sealplatform.sdk.config.HttpConnectionConfig;
import com.timvale.sealplatform.sdk.config.ProjectConfig;
import com.timvale.sealplatform.sdk.file.SignLocalClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 描述：签章工具类
 *
 * @Author zhengqiang
 * @Date 2023/3/6 17:53
 **/
@Component
public class SignLocalClientUtil {

    /**
     * 是否测试环境
     */
    //未取到值默认是false
    private static boolean isTest;

    @Value("${isTest}")
    private void setIsPrd(boolean isTest) {
        this.isTest = isTest;
    }

    private static SignLocalClient signLocalClient = getClient();

    /**
     * 是否第一次初始化
     */
    private static boolean isFirstInit = true;

    public static SignLocalClient getClient() {
        if (signLocalClient != null) {
            if (isTest && isFirstInit) { //当前为测试环境及是第一次加载签章客户端,重新初始化
                ProjectConfig projectConfig = new ProjectConfig();
                //todo 先写死
                projectConfig.setAppId("330000453");
                projectConfig.setSecret("be503d15b62345989849c10ec76edcc0");
                //本地使用
                projectConfig.setDomainName("zjyz.zjzwfw.gov.cn:9999");
                HttpConnectionConfig httpConnectionConfig = new HttpConnectionConfig();
                httpConnectionConfig.setProtocol("http");
                httpConnectionConfig.setConnectionTimeOut(3000);
                signLocalClient = ClientManageService.initClient(projectConfig, httpConnectionConfig);
                isFirstInit = false;
            }
            return signLocalClient;
        } else {
            ProjectConfig projectConfig = new ProjectConfig();
            //todo 先写死
            projectConfig.setAppId("330000453");
            projectConfig.setSecret("be503d15b62345989849c10ec76edcc0");
            projectConfig.setDomainName("10.145.147.112:8581/proxy/dzyzAddr/");
            HttpConnectionConfig httpConnectionConfig = new HttpConnectionConfig();
            httpConnectionConfig.setProtocol("http");
            httpConnectionConfig.setConnectionTimeOut(3000);
            return ClientManageService.initClient(projectConfig, httpConnectionConfig);
        }
    }

}
